import {host} from '../../config.js'
import axios from 'axios'
import { ElMessage } from 'element-plus'
// import store from '@/store'
const service = axios.create({
  baseURL: host(),
  timeout: 5000
})

service.interceptors.request.use(
  (config) => {
    if (localStorage.getItem('token')) {
      config.headers.token = localStorage.getItem('token')
    }
    // console.log(config)
    return config
  },
  (error) => {
    return Promise.reject(new Error(error))
  }
)

service.interceptors.response.use(
  (response) => {
    const { data, code, msg } = response.data
    if (code === 200) {
      return data
    } else {
      ElMessage.error(msg)
      return Promise.reject(new Error(msg))
    }
  },
  (error) => {
    console.log(error.response)
    error.response && ElMessage.error(error.response.data)
    return Promise.reject(new Error(error.response.data))
  }
)
export default service
